package org.example.server;

import io.vertx.core.Vertx;

public class VertxHttpServer implements HttpServer {
    @Override
    public void doStart(int port) {
        // 创建 Vert.x 实例
        Vertx vertx = Vertx.vertx();
        // 创建 HTTP 服务器
        io.vertx.core.http.HttpServer vertxHttpServer = vertx.createHttpServer();
        // 监听端口并返回请求
//        vertxHttpServer.requestHandler(request -> {
//            System.out.println("Received request from client: " + request.method() + " " + request.uri());
//            // 发送响应并结束请求
//            request.response().putHeader("content-type", "text/plain")
//                    .end("Hello World from Vert.x!");
//        });
        vertxHttpServer.requestHandler(new HttpServerHandler());
        // 启动 HTTP 服务器并监听端口
        vertxHttpServer.listen(port, result -> {
            if (result.succeeded()) {
                System.out.println("HTTP server started on port " + port);
            } else {
                System.out.println("HTTP server start failed: " + result.cause());
            }
        });
    }
}
